package com.helloworld.chat.action;

import com.helloworld.chat.entity.Friend;
import com.helloworld.chat.entity.FriendRequest;
import com.helloworld.chat.entity.Group;
import com.helloworld.chat.entity.User;
import com.helloworld.chat.service.FriendService;
import com.opensymphony.xwork2.ModelDriven;
import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by xdzy on 2017/6/29.
 */
@Controller
@Scope("prototype")
public class FriendAction implements ModelDriven<FriendRequest>{
    private FriendRequest friendRequest=new FriendRequest();
    private String friendName;
    private int id;
    private String alias;
    @Autowired
    private FriendService friendService;
    private String result;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getAlias() {
        return alias;
    }

    public void setAlias(String alias) {
        this.alias = alias;
    }

    public String getResult() {
        return result;
    }

    public void setFriendService(FriendService friendService) {
        this.friendService = friendService;
    }

    public String getFriendName() {
        return friendName;
    }

    public void setFriendName(String friendName) {
        this.friendName = friendName;
    }

    public String addFriendForm(){

        return "addFriendForm";
    }

    public String addFriend(){
        HttpServletRequest request= ServletActionContext.getRequest();
        User user= (User) request.getSession().getAttribute("loginUser");
        result=friendService.addFriend(friendRequest,user,friendName);
        return "success";
    }
    public String friendList(){
        HttpServletRequest request= ServletActionContext.getRequest();
        String t= request.getParameter("groupId");
        if(t==null){
            result="error";
        }else{
            result=friendService.getFriendList_JSON(Integer.parseInt(t));
        }
        return "success";
    }

    public String deleteFriend(){
        friendService.delete(id);
        result="success";
        return "success";
    }

    public String updateAlias(){
        friendService.update(alias,id);
        result="success";
        return "success";
    }

    @Override
    public FriendRequest getModel() {
        //friendRequest.setGroup(new Group());
        return friendRequest;
    }
}
